from peewee import *


db = MySQLDatabase('nemtocr', host='localhost', port=3800, user='root', password='!@mtg23YWZ^&')


class UniversityScoreRank(Model):
    id = PrimaryKeyField()
    year = IntegerField()
    batch = IntegerField()
    type = IntegerField()
    code = CharField(max_length=32)
    name = CharField(max_length=64)
    highest_score = CharField(max_length=32)
    highest_rank = CharField(max_length=32)
    lowest_score = IntegerField()
    lowest_rank = IntegerField()
    mean_score = IntegerField()
    mean_rank = IntegerField()
    plan_number = IntegerField()
    submit_number = IntegerField()
    enter_number = IntegerField()
    province = CharField(max_length=16)
    city = CharField(max_length=16)
    province_code = IntegerField()
    city_code = IntegerField()
    nature_name = IntegerField()
    f211 = IntegerField()
    f985 = IntegerField()
    dual_class_name = CharField(max_length=32)
    score = TextField()
    is_deleted = IntegerField()

    class Meta:
        database = db  # This model uses the "spider" database.
        table_name = 'university_score_rank'  # 如果不指定，默认是person


    # 插入单条数据(返回新增id)
    def toInsert(self, row):
        try:
            return UniversityScoreRank.insert(row).execute()
        except Exception as e:
            print(e)
            return ''


    # 批量插入数据
    def toInsertBatch(self, rows):
        with db.atomic():
            UniversityScoreRank.insert_many(rows).execute()


    # 根据id，获取单条记录
    def getOne(self, id):
        try:
            return UniversityScoreRank.select().where(UniversityScoreRank.id == id).get()
        except Exception as e:
            return ''


    # 根据年份查询列表
    def getList(self, year):
        try:
            return UniversityScoreRank.select().where(UniversityScoreRank.year == year).dicts().execute()
        except Exception as e:
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getOneByYearBatch(self, year, batch, type, code):
        try:
            return UniversityScoreRank.select().where((UniversityScoreRank.year == year) &
                                                      (UniversityScoreRank.batch == batch) &
                                                      (UniversityScoreRank.type == type) &
                                                      (UniversityScoreRank.code == code)).dicts().get()
        except Exception as e:
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getCount(self, year, batch, type, code):
        try:
            return UniversityScoreRank.select().where((UniversityScoreRank.year == year) &
                                                      (UniversityScoreRank.batch == batch) &
                                                      (UniversityScoreRank.type == type) &
                                                      (UniversityScoreRank.code == code)).count()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdate(self, row, id):
        try:
            return UniversityScoreRank.update(
                year=row['year'], batch=row['batch'],
                type=row['type'], code=row['code'], name=row['name'],
                lowest_score=row['lowest_score'], lowest_rank=row['lowest_rank'],
                mean_score=row['mean_score'], mean_rank=row['mean_rank'], score=row['score'],
                plan_number=row['plan_number'], submit_number=row['submit_number'], enter_number=row['enter_number'],
                province=row['province'], city=row['city'], province_code=row['province_code'], city_code=row['city_code'],
                nature_name=row['nature_name'], f211=row['f211'], f985=row['f985'], dual_class_name=row['dual_class_name']
            ).where(UniversityScoreRank.id == id).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateRank(self, row, id):
        try:
            return UniversityScoreRank.update(
                lowest_rank=row['lowest_rank'], mean_rank=row['mean_rank']
            ).where(UniversityScoreRank.id == id).execute()
        except Exception as e:
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getRowsByYearBatch(self, year1, year2, batch, type, code):
        try:
            return UniversityScoreRank.select().where((UniversityScoreRank.year >= year1) &
                                                      (UniversityScoreRank.year <= year2) &
                                                      (UniversityScoreRank.code == code) &
                                                      (UniversityScoreRank.batch == batch) &
                                                      (UniversityScoreRank.type == type)).order_by(UniversityScoreRank.year.desc()).dicts().execute()
        except Exception as e:
            return ''


if __name__ == '__main__':
    universityScoreRank = UniversityScoreRank()
    count = universityScoreRank.getRowsByYearBatch(2018, 2020, 9, 1, 10001)
    if count:
        print(count)
    else:
        print("空")